package com.ler.fast.factory;

import com.ler.fast.entity.UserAccount;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.wiring.BeanWiringInfo;
import org.springframework.stereotype.Component;

/**
 * @author lww
 * @date 2020-07-17 16:54
 */
//@Component
public class CustomBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        //创建BeanDefinitionBuilder
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(UserAccount.class);
        //设置属性值
        builder.addPropertyValue("userName", "CustomBeanDefinitionRegistryPostProcessor");
        //设置可通过@Autowire注解引用
        builder.setAutowireMode(BeanWiringInfo.AUTOWIRE_BY_NAME);
        //注册到BeanDefinitionRegistry
        registry.registerBeanDefinition("userAccount", builder.getBeanDefinition());
        /*BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DruidDataSource.class);
        builder.addPropertyValue("driverClassName", "com.mysql.jdbc.Driver");
        builder.addPropertyValue("url", "jdbc:mysql://127.0.0.1:3306/ask?useUnicode=true&useSSL=false&characterEncoding=utf8");
        builder.addPropertyValue("username", "root");
        builder.addPropertyValue("password", "adminadmin");
        builder.addPropertyValue("validationQuery", "SELECT 1");
        builder.addPropertyValue("initialSize", "1");
        builder.addPropertyValue("maxActive", "20");
        builder.addPropertyValue("minIdle", "1");
        builder.addPropertyValue("maxWait", "60_000");
        builder.addPropertyValue("poolPreparedStatements", "true");
        builder.addPropertyValue("maxPoolPreparedStatementPerConnectionSize", "20");
        builder.addPropertyValue("timeBetweenEvictionRunsMillis", "60_000");
        builder.addPropertyValue("minEvictableIdleTimeMillis", "300_000");
        builder.setAutowireMode(BeanWiringInfo.AUTOWIRE_BY_NAME);
        registry.registerBeanDefinition("dataSource", builder.getBeanDefinition());*/

        /*
          Exception encountered during context initialization - cancelling refresh attempt:
          org.springframework.beans.factory.support.BeanDefinitionOverrideException:
          Invalid bean definition with name 'dataSource' defined in null:
          Cannot register bean definition [Root bean: class [com.alibaba.druid.pool.DruidDataSource];
          scope=; abstract=false; lazyInit=null; autowireMode=1; dependencyCheck=0; autowireCandidate=true;
          primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null;
          destroyMethodName=null] for bean 'dataSource':
          There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0;
          autowireCandidate=true; primary=false;
          factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari;
          factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred);
          defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] bound.
        */
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
